package net._708s.logmanager.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class MyParam {  
  //  private static Logger logger = Logger.getLogger(SystemParameter.class); 
    private String filePath = Thread.currentThread().getContextClassLoader().getResource("").getPath()+"./application.properties";
    private final Properties properties;  
    public static Map<String, String> proMap=new HashMap<String, String>();
    private static Map<String, MyParam> myParamMap=new HashMap<String, MyParam>();
    
    
    public static synchronized MyParam getInstance(String filePath){
    	MyParam myParam =myParamMap.get(filePath);
    	
    	if(myParam == null){
    		myParam = new MyParam(filePath);
    		myParamMap.put(filePath, myParam);
    	}
    	
    	return myParam;
    }
    
    public MyParam(String filePath) {
    	this.filePath = filePath;
    	
        properties = new Properties();  
        FileInputStream fis = null;  
        try {
        	File file = new File(filePath);
        	
        	if(!file.exists()){//如果文件不存在则新建
        		if (!file.getParentFile().exists()) {
        			file.getParentFile().mkdirs();
        		}
        		
        		file.createNewFile();
        	}
        	
            fis = new FileInputStream(file);  
            properties.load(fis);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if(fis != null) fis.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    
    	
    }  
    
    public static void main(String[] args){
    	String path = Thread.currentThread().getContextClassLoader().getResource("").getPath(); 
    	MyParam mySystemParameter = MyParam.getInstance(path+"/application.properties");   	
    	System.out.println(mySystemParameter.getPropertie("index.url"));
    }
    
    public static String getDefaultIndexUrl(){
    	String path = Thread.currentThread().getContextClassLoader().getResource("").getPath(); 
    	MyParam mySystemParameter = MyParam.getInstance(path+"/application.properties"); 
    	return mySystemParameter.getPropertie("index.url");
    }
      
    public synchronized void modifyPropertie(String key,String value) {  
        properties.setProperty(key, value);  
        
        FileOutputStream fos = null;  
        try {  
            fos = new FileOutputStream(new File(filePath));  
            properties.store(fos, "");  
            proMap.put(key, value);  
            fos.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }
    }  
    
    public String getPropertie(String key){
    	FileInputStream fis = null; 
    	String value = null;
    	try {  
            fis = new FileInputStream(new File(filePath));  
            properties.load(fis);  
            value = properties.getProperty(key);  
            
    	} catch(Exception ex){
    		ex.printStackTrace();
    	}finally {  
    		if(fis != null)
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
        }  
    	
		return value;
    }
}  